package map;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public class ForEachDemo {
    public static void main(String[] args) {
        Map<String,Integer> map=new LinkedHashMap<>();
        map.put("语文",95);
        map.put("数学",100);
        map.put("英语",80);
        map.put("物理",86);
        map.put("化学",90);
        System.out.println(map);//{语文=95, 数学=100, 英语=80, 物理=86, 化学=90}
        /*
        //遍历key
        Set<String> keySet=map.keySet();
        keySet.forEach(new Consumer<String>() {
            public void accept(String s) {
                System.out.println(s);
            }
        });

        //遍历value
        Collection<Integer> values=map.values();
        values.forEach(new Consumer<Integer>() {
            @Override
            public void accept(Integer v) {
                System.out.println(v);
            }
        });
        //遍历键值对
        Set<Map.Entry<String,Integer>> entrySet=map.entrySet();
        entrySet.forEach(new Consumer<Map.Entry<String, Integer>>() {
            @Override
            public void accept(Map.Entry<String, Integer> e) {
                System.out.println("key="+e.getKey()+",value="+e.getValue());
            }
        });
        map.forEach(new BiConsumer<String, Integer>() {
            @Override
            public void accept(String s, Integer v) {
                System.out.println(s+","+v);
            }
        });
        */
        //单列集合的forEach
        Set<String> keySet=map.keySet();
        keySet.forEach(s-> System.out.println(s));
        Collection<Integer> values=map.values();
        values.forEach(v-> System.out.println(v));
        Set<Map.Entry<String,Integer>> entrySet=map.entrySet();
        entrySet.forEach(e-> System.out.println("key="+e.getKey()+",value="+e.getValue()));
        //双列集合的forEach
        map.forEach((s,v)-> System.out.println(s+","+v));
    }
}
